<?php

/* * ************************************************************************* 
 * item_trends_model.class.php
 * Encoding : UTF-8
 * Desc. :商品走势表数据库实现类
 * Created on : 2013-5-14 13:58:44 by Aiden , aiden.ke@doosnet.net
 * Updated on : 2013-5-14 13:58:44 by Aiden , aiden.ke@doosnet.net
 * ************************************************************************* */
defined('IN_SPIDER') or exit('No permission resources.');

pc_base::load_sys_class('model', '', 0);

class item_trends_model extends model {

    public function __construct() {
        $this->db_config = pc_base::load_config('database');
        $this->db_setting = 'default';
        $this->table_name = '';
        parent::__construct();
    }

    /**
     * 设置商品数据表
     * @param string $tablename 商城名称
     */
    public function table_name($mallname) {
        $this->table_name = $this->db_config[$this->db_setting]['tablepre'] . $mallname;
        return $this->table_name;
    }

    /**
     * 更新走势表信息（更新周期每天，保留30个节点）
     * @param string $table_name 表名
     * @param int $mall_id 来源ID
     * @param int $item_no 产品ID
     * @param int $price 价格
     * @param int $review 价格
     * @param int $score 价格
     * @return int trend 价格差
     */
    public function update_trends_table($table_name, $mall_id, $item_no, $price, $reviews, $score) {
        $trend = 0;
        $time = getmicrotime(TRUE);

        $sql = "select * ,count(*) as cnt from `$table_name` where `item_no`='$item_no' order by `last_update` DESC;";
        $result = $this->query($sql);

        if ($result['0']['cnt'] == 0) {
            if (is_numeric($price) && $price > 0) {
                $sql = "insert into `$table_name` set `item_no`=$item_no,`goods_price`='$price',`reviews`='$reviews',`score`='$score',`trend`='$trend',last_update='$time';";
                $this->query($sql);
            } else {
                echo'价格为空,跳过更新' . "<hr/>";
            }
        } else {
            $is_update = $time - $result[0]['last_update'] > intval(pc_base::load_config('system', 'cycleTime')) ? TRUE : FALSE;
            if ($is_update) {//判断当天是否已更新过价格，若是，则跳过。
                
                //价格
                $price = $price > 0 ? $price : $result[0]['goods_price'];
                
                //销量
                $reviews = $reviews > 0 ? $reviews : $result[0]['reviews'];
                
                //评分
                $score = $score > 0 ? $score : $result[0]['score'];

                $trend = $price - $result[0]['goods_price'];

                //若大于结点数，则移除第一个;
                if ($result[0]['cnt'] >= pc_base::load_config('system', 'price_nums')) {
                    $sql = "DELETE FROM `$table_name` WHERE `item_no`=$item_no ORDER BY last_update ASC limit 1;";
                    $this->query($sql);
                }
                
                $sql = "insert into `$table_name` set `item_no`=$item_no,`goods_price`='$price',`reviews`='$reviews',`score`='$score',`trend`='$trend',last_update='$time';";
                $this->query($sql);
            } else {
                echo '已经更新过此ID:' . $item_no . "<hr/>";
            }
        }
        unset($time, $sql, $result, $is_update, $price, $reviews, $score);
        return $trend;
    }

}

?>
